Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
How do I click a javascript "link" using rselenium to start downloading excel file?

I'm a beginner in R and I've been trying to scrape the following website:

https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&

to automatically start downloading an excel file from this air monitoring station here in Chile. However, I noticed that using SelectorGadget the download link couldn't be selected and found out it was because the link was a javascript link (href="javascript:Open('xcl');">Excel CSV) as seen in the image below.

enter image description here

My question is there any way I can simulate a click on that javascript link? I've already simulated clicks for other parts of the same website succesfully but they where not javascript links. This is what I've already tried:

library(rvest)
library(RSelenium)


rD <- rsDriver(browser="firefox", port=4040L, verbose=F)
remDr <- rD[["client"]]


remDr$open()
remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&")
Sys.sleep(5) # give the page time to fully load


# click on excel link to start downloading file (doesnt work) ------------

xls <- remDr$findElement("xpath", "/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a")
xls$clickElement()
/html/body/table/tbody/tr/td/table[2]/tbody/tr[1]/td/label/span[3]/a


##### TESTING

remDr$navigate("https://sinca.mma.gob.cl/cgi-bin/APUB-MMA/apub.htmlindico2.cgi?page=pageFrame&header=Las%20Condes&macropath=./RM/D13/Cal/0001&macro=0001.diario.diario&from=970508&to=090312&")
excel <- remDr$findElement(using = "css selector", "[href*=javascript]")
#excel <- remDr$findElement(using = "css selector", '[href*="javascript:Open('xcl')"]')
excel$highlightElement() # to visually check what elemnet is selected


remDr$closeServer()
remDr$close()

My objective is to be able to click the link and start downloading the excel file. I've been trying to do this for a couple days and I'm stuck, so any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is inside a frame that is inside a frameset. You first need to switch to that frame via the parent frameset, using RSelenium. You can use the frame ids frMain and right to target the elements to switch to. Then you should be able to simply target that href by using a css attribute = value selector of [href*=xcl]. The * is the contains operator specifying that the href attribute value of the target contains the substring xcl.

Something like the following (not tested):

frame_set <- remDr$findElement("css", "#frMain")
remDr$switchToFrame(frame_set)

child_frame <- remDr$findElement("css", "#right")
remDr$switchToFrame(child_frame)

csv_download <- remDr$findElement(using = "css", "[href*=xcl]")
csv_download$clickElement()

You can use browser dev tools F12 to view that path:

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!